package com.fxsh168.server.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.fxsh168.server.R;
import com.fxsh168.server.base.BaseActivity;
import com.fxsh168.server.entitys.PushOrdersEntity;
import com.fxsh168.server.entitys.RegiYZMCG;
import com.fxsh168.server.interfaces.Constant;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;


public class NearOrderDetailActivity extends BaseActivity {
    TextView mOrderNum, mStartDetails, mEndDetails, mTimeDetails,
            teRoute, mIncomeDetails, mStateDetails;
    private String mPhoneMap, tokenMap, ddNumbers;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_near_order_details);
        mOrderNum = (TextView) findViewById(R.id.orderNum);//订单号
        mStartDetails = (TextView) findViewById(R.id.startDetails);//起点
        mEndDetails = (TextView) findViewById(R.id.endDetails);//终点
        mTimeDetails = (TextView) findViewById(R.id.timeDetails);//时间
        mIncomeDetails = (TextView) findViewById(R.id.incomeDetails);//收入
        mStateDetails = (TextView) findViewById(R.id.stateDetails);//订单状态
        teRoute = (TextView) findViewById(R.id.teRoute);
        SharedPreferences sp = getSharedPreferences(Constant.LOGINCLASS, MODE_PRIVATE);
        mPhoneMap = sp.getString("mPhone", "");
        tokenMap = sp.getString("token", "");
        getIntentInfo();
    }

    private void getIntentInfo() {
        //获取从前页传递过来的数据
        Intent intent = getIntent();
        if (intent != null) {
            String action = intent.getAction().trim();
            Bundle bundle = intent.getBundleExtra("bundle");
            int position = bundle.getInt("position");
            PushOrdersEntity pushOrdersEntity = (PushOrdersEntity) bundle.getSerializable("wholeOrderEntity");
            List<PushOrdersEntity.DataBean> data3 = pushOrdersEntity.getData();

            PushOrdersEntity.DataBean dataBean = data3.get(position);
            String city = dataBean.getDown().toString().trim();
            String destnation = dataBean.getAddresses().toString().trim();
            String gotime = dataBean.getGotime().toString().trim();
            String adress = dataBean.getAddresses().toString().trim();
            String down = dataBean.getDown().toString().trim();
            ddNumbers = dataBean.getId().toString();
            mOrderNum.setText(dataBean.getNmber().toString());
            //将数据设置到控件上
            mStartDetails.setText(adress);
            mEndDetails.setText(down);
            teRoute.setText("普通线路");
            mStateDetails.setText("已下单");
            mTimeDetails.setText(gotime);
            //发车的点击事件
            mIncomeDetails.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    agreeOrders();
                }
            });
        }
    }

    //    接单
    public void agreeOrders() {
        OkHttpUtils.post().addParams("phone", mPhoneMap).addParams("token", tokenMap)
                .addParams("id", ddNumbers).url(Constant.ORDERECIVEREQUESTPATH)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast("连接不上服务器!");
            }

            @Override
            public void onResponse(String response, int id) {
                if (response != null) {
                    RegiYZMCG regiYZMCG = new Gson().fromJson(response, RegiYZMCG.class);
                    String code = regiYZMCG.getCode().trim();
                    if (code.equals(200 + "")) {
                        Intent inten = new Intent(NearOrderDetailActivity.this, MainActivity.class);
                        startActivity(inten);
                        finish();
                    } else if (code.equals(201 + "")) {

                    } else if (code.equals(202 + "")) {

                    } else if (code.equals(203 + "")) {
                        showToast("接单失败!");
                    } else if (code.equals(222 + "")) {
                        Toast.makeText(NearOrderDetailActivity.this, "附近司机已接单!", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}
